package com.example.sharding.service.impl;

import com.example.sharding.dao.UserRepository;
import com.example.sharding.service.ExampleService;
import com.example.sharding.service.UserService;
import com.example.sharding.vo.User;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by mazeyi on 2019/12/3.
 */

@Service
public class UserServiceImpl implements UserService {


    @Resource
    UserRepository userRepository;
    @Override
    public void initEnvironment() throws SQLException {

    }

    @Override
    public void cleanEnvironment() throws SQLException {

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void processSuccess() throws SQLException {
        try {
            TransactionTypeHolder.set(TransactionType.XA);
            System.out.println("-------------- Process Success Begin ---------------");

            int num = new Random().nextInt(9999);
            User user = new User();
            user.setUserId(num);
            user.setSex((byte) 1);
            user.setPwd("aaaabbaca");
            user.setUserName("durant");
            userRepository.save(user);

            int num2 = new Random().nextInt(9999);

            User user2 = new User();
            user2.setUserId(num2);
            user2.setSex((byte) 0);
           // 测试分布式事务 int i = 10 / 0;
            user2.setPwd("AAAAAAA");
            user2.setUserName("KIKI");
            userRepository.save(user2);

            System.out.println("-------------- Process Success Finish --------------");
        }catch (Exception e){
            e.printStackTrace();
            // 加上这个来保证回滚 否则不会回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            TransactionTypeHolder.clear();
            TransactionAspectSupport.currentTransactionStatus().flush();

        }

    }

    @Override
    public void processFailure() throws SQLException {
        System.out.println("-------------- Process Failure Begin ---------------");
        System.out.println("-------------- Process Failure Finish --------------");
        throw new RuntimeException("Exception occur for transaction test.");
    }


    private void deleteData(final List<Long> userIds) {
//        System.out.println("---------------------------- Delete Data ----------------------------");
//        for (Long each : userIds) {
//            userRepository.delete(each);
//        }
    }

    @Override
    public void printData() {
        System.out.println("---------------------------- Print User Data -----------------------");
//        for (Object each : userRepository.selectAll()) {
//            System.out.println(each);
//        }
    }

    @Override
    public User insert(User user) {
        System.out.println("---------------------------- Insert Data ----------------------------");
        List<Long> result = new ArrayList<>(10);
        for (int i = 1; i <= 10; i++) {
            user.setUserId(i);
            user.setUserName("test_jpa_" + i);
            user.setPwd("pwd_jpa_" + i);
            userRepository.save(user);
            result.add((long) user.getUserId());
        }
        return  user;
    }

    @Override
    public User findByUserName(String name) {
       return userRepository.findByUserName(name);
    }
}